#include <iostream>
#include <string>
#include <cstring>

using namespace std;

const int N = 100010;

int p[N];
int Size[N];

int n, m;

int find(int x)
{
    if (p[x] != x) p[x] = find(p[x]);
    return p[x];
}

int main()
{
    cin >> n >> m;

    for (int i = 0; i <= n; ++i)
    {
        p[i] = i;
        Size[i] = 1;
    }

    for (int i = 0; i < m; ++i)
    {
        string op;;
        cin >> op;

        if (op == "C")
        {
            int a, b; cin >> a >> b;
            int pa = find(a), pb = find(b);
            if (pa != pb)
            {
                p[pa] = pb;
                Size[pb] += Size[pa];
            }
        }
        else if (op == "Q1")
        {
            int a, b; cin >> a >> b;
            int pa = find(a), pb = find(b);
            if (pa != pb)
            {
                cout << "No" << endl;
            }
            else
            {
                cout << "Yes" << endl;
            }
        }
        else
        {
            int a; cin >> a;
            int pa = find(a);

            cout << Size[pa] << endl;
        }
    }
    return 0;
}